package com.hzya.frame.system.dictionaryshop.service.impl;

import com.hzya.frame.system.dictionaryshop.dao.IDictionaryShopDao;
import com.hzya.frame.system.dictionaryshop.entity.DictionaryShopEntity;
import com.hzya.frame.system.dictionaryshop.service.IDictionaryShopService;
import com.hzya.frame.system.user.entity.UserEntity;
import com.hzya.frame.utils.core.action.SessionContext;
import com.hzya.frame.utils.core.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 数据字典  service
 * @tableName: system_dictionaryshop
 * @entityName: DictionaryShopEntity
 * @author: gjh
 * @history: 1.0
 */
@Service(value = "system_dictionaryshopService")
public class DictionaryShopServiceImpl extends BaseService<DictionaryShopEntity, Long> implements IDictionaryShopService {

    protected IDictionaryShopDao dictionaryshopDao;

    @Autowired
    public void setDictionaryShopDao(IDictionaryShopDao dao) {

        this.dictionaryshopDao = dao;
        this.dao = dao;
    }

    @Override
    public List<DictionaryShopEntity> query(DictionaryShopEntity dictionaryDomain) {

        List<DictionaryShopEntity> list = dictionaryshopDao.query(dictionaryDomain);
        return list;
    }

    @Override
    public DictionaryShopEntity update(DictionaryShopEntity dictionaryDomain) throws Exception {
        super.update(dictionaryDomain);
        return dictionaryDomain;
    }

    @Override
    public void batchSave(List<DictionaryShopEntity> sysconfigList) throws Exception {
        // TODO Auto-generated method stub
        if (null != sysconfigList && 0 <= sysconfigList.size()) {
            for (DictionaryShopEntity tempDomain : sysconfigList) {
                dictionaryshopDao.update(tempDomain);

            }
        }

    }

    @Override
    public DictionaryShopEntity saveMulit(DictionaryShopEntity dictionaryDomain) throws Exception {
        UserEntity user = SessionContext.getCurrentUser();
        dictionaryDomain.setCreate_user_id(user.getId());
        dictionaryDomain.setModify_user_id(user.getId());
        dictionaryshopDao.save(dictionaryDomain);
        List<DictionaryShopEntity> tempList = dictionaryDomain.getSysconfigList();
        if (null != tempList && 0 <= tempList.size()) {
            for (DictionaryShopEntity tempDomain : tempList) {
                tempDomain.setTab_name(dictionaryDomain.getTab_name());
                tempDomain.setColumn_name(dictionaryDomain.getColumn_name());
                tempDomain.setMemo(dictionaryDomain.getMemo());
                tempDomain.setCreate_user_id(dictionaryDomain.getCreate_user_id());
                tempDomain.setCreate_user_id(user.getId());
                tempDomain.setModify_user_id(user.getId());
                dictionaryshopDao.save(tempDomain);
            }
        }
        return dictionaryDomain;
    }

    public int removeById(Long id) throws Exception {
        return dictionaryshopDao.logicRemove(id);
    }

    /**
     * 查询指定tab_name,column_name的数据字典
     *
     * @param dictionaryDomain
     * @return
     */
    @Override
    public List<DictionaryShopEntity> queryDictionaries(DictionaryShopEntity dictionaryDomain) {
        // TODO Auto-generated method stub
        return dictionaryshopDao.queryList(dictionaryDomain, "DictionaryShopEntity_query_dictionaries");
    }

    @Override
    public DictionaryShopEntity querySelfDictionary(String tab_name, String column_name, String column_value) {
        DictionaryShopEntity domain = new DictionaryShopEntity();
        domain.setTab_name(tab_name);
        domain.setColumn_name(column_name);
        domain.setColumn_value(column_value);
        List<DictionaryShopEntity> tempList = queryDictionaries(domain);
        return tempList == null ? null : tempList.get(0);
    }

    @Override
    public List<DictionaryShopEntity> queryDictionaries(String tab_name, String column_name) {
        DictionaryShopEntity paramDomain = new DictionaryShopEntity();
        paramDomain.setTab_name(tab_name);
        paramDomain.setColumn_name(column_name);
        return queryDictionaries(paramDomain);
    }

    @Override
    public List<DictionaryShopEntity> getTypeList() {
        return dictionaryshopDao.queryList(null, "DictionaryShopEntity_selectType");
    }

    public Map<String, DictionaryShopEntity> createDictionaryCacheMap() {
        Map<String, DictionaryShopEntity> resultMap = new HashMap<String, DictionaryShopEntity>();
        List<DictionaryShopEntity> dataList = dictionaryshopDao.getAll();
        for (DictionaryShopEntity tempDomain : dataList) {
            resultMap.put(
                    tempDomain.getTab_name() + "#" + tempDomain.getColumn_name() + "#" + tempDomain.getColumn_value(),
                    tempDomain);
        }
        return resultMap;
    }

    @Override
    public int logicRemove(Long id) throws Exception {
        // TODO Auto-generated method stub
        int result = super.logicRemove(id);
        return result;
    }

    @Override
    public List<DictionaryShopEntity> queryColumn_name(DictionaryShopEntity domain) {
        return dictionaryshopDao.queryColumn_name(domain);
    }
    /** 获取数据字典大类*/
    @Override
    public List<DictionaryShopEntity> ListGetTypeList(){
        return  dictionaryshopDao.ListGetTypeList();
    }
}
